﻿using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.AspNetCore.Mvc.Controllers;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace Lrd.Template.Core.Infrastructure.Extensions
{
    /// <summary>
    /// 控制器扩展
    /// </summary>
    public static partial class Extensions
    {
        /// <summary>
        /// 得到特性
        /// </summary>
        /// <typeparam name="TAttribute"></typeparam>
        /// <param name="actionModel"></param>
        /// <param name="inherit"></param>
        /// <returns></returns>
        public static TAttribute GetAttribute<TAttribute>(this ActionModel actionModel, bool inherit = true) where TAttribute : Attribute
        {

            return actionModel.Controller.ControllerType.GetCustomAttribute<TAttribute>(inherit);
        }


        /// <summary>
        /// 得到控制类型
        /// </summary>
        /// <param name="actionModel"></param>
        /// <returns></returns>
        public static TypeInfo GetControllerType(this ActionModel actionModel)
        {
            return actionModel.Controller.ControllerType;
        }

        /// <summary>
        /// 转成类型信息
        /// </summary>
        /// <param name="controllerActionDescriptor"></param>
        /// <returns></returns>
        public static TypeInfo AsTypeInfo(this ControllerActionDescriptor controllerActionDescriptor)
        {
            return controllerActionDescriptor.ControllerTypeInfo;
        }
    }
}
